﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ZOIL.Library.Components.LazyLoadingContainer
{
    /// <summary>
    /// Interaction logic for FileSystemContainer.xaml
    /// </summary>
    public partial class FileSystemContainer : LazyLoadingContainer, INotifyPropertyChanged
    {
        // Using a DependencyProperty as the backing store for Folder.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FolderProperty =
            DependencyProperty.Register("Folder", typeof(DirectoryInfo), typeof(FileSystemContainer), new UIPropertyMetadata(null, OnFolderChanged));

        private String _folderName;

        private ObservableCollection<DirectoryInfo> _subfolder;

        public FileSystemContainer()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }
        [TypeConverter(typeof(DirectoryInfoConverter))]
        public DirectoryInfo Folder
        {
            get { return (DirectoryInfo)GetValue(FolderProperty); }
            set { SetValue(FolderProperty, value); }
        }

        public String FolderName
        {
            get { return _folderName; }
            set
            {
                _folderName = value;
                RaisePropertyChangedEvent("FolderName");
            }
        }

        public ObservableCollection<DirectoryInfo> Subfolder
        {
            get { return _subfolder; }
            set
            {
                _subfolder = value;
                RaisePropertyChangedEvent("Subfolder");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChangedEvent(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            DataContext = this;
        }

        private static void OnFolderChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != null)
            {
                (sender as FileSystemContainer).FolderName = (args.NewValue as DirectoryInfo).Name;
            }
            try
            {
                (sender as FileSystemContainer).Subfolder = new ObservableCollection<DirectoryInfo>((args.NewValue as DirectoryInfo).GetDirectories());
            }
            catch
            {
            }
        }
    }

    public class DirectoryInfoConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var text = value as string;
            if (text != null)
            {
                try
                {
                    var info = new DirectoryInfo(text);
                    return info;
                }
                catch
                {
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}
